package com.example.wsappproject.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.wsappproject.R;
import com.example.wsappproject.practice.ArticleEntity;

import java.util.List;


public class ArticleAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    //mList是一个引用，存储的是其对象的内存地址
    //真正的对象还在内存中，独一份
    private List<ArticleEntity.DataBean.DatasBean> mList;

    private View mBannerView;

    private final static int BANNER_VIEW_TYPE = 0;
    private final static int ITEM_VIEW_TYPE = 1;

    public int getItemViewType(int position) {
        if (position == 0){
            return BANNER_VIEW_TYPE;
        }else {
            return ITEM_VIEW_TYPE;
        }
    }

    /**
     * 址传递：传递的是指向某个对象的内存地址
     * @param Context 址传递
     * @param List    址传递
     * @param mBannerView
     */
    public ArticleAdapter2(Context Context, List<ArticleEntity.DataBean.DatasBean> List, View mBannerView) {
        this.mContext = Context;
        this.mList = List;
        this.mBannerView = mBannerView;
    }

    /**
     * 创建View视图的ViewHolder
     * @param parent
     * @param viewType
     * @return
     */
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType){
            case BANNER_VIEW_TYPE:
                return new BannerViewHolder(mBannerView);
            case ITEM_VIEW_TYPE:
                LayoutInflater inflater = LayoutInflater.from(mContext);
                //parent换成null，有可能导致界面展示异常
                View view = inflater.inflate(R.layout.item_article_adapter2, parent, false);
                return new ArticleViewHolder(view);
        }

        return null;
    }

    /**
     * 设置数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if(itemViewType == BANNER_VIEW_TYPE){
            //啥也不干
            //设置mBannerView包裹控件的数据，不在此处进行
        }else if(itemViewType == ITEM_VIEW_TYPE){
            ArticleViewHolder articleViewHolder = (ArticleViewHolder) holder;
            //先获取position位置的数据实体对象
            ArticleEntity.DataBean.DatasBean datasBean = mList.get(position - 1);
            articleViewHolder.tvAuthor.setText(datasBean.getAuthor());
            articleViewHolder.tvClassify.setText(datasBean.getSuperChapterName() + "/" + datasBean.getChapterName());
            articleViewHolder.tvTitle.setText(datasBean.getTitle());
            articleViewHolder.tvTime.setText(datasBean.getNiceDate());
        }
    }

    /**
     * 返回条目的数量
     * 返回为0，则一个条目都不展示
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    /**
     * 找小朋友，找控件
     */
    static class ArticleViewHolder extends RecyclerView.ViewHolder {

        TextView tvAuthor;
        TextView tvClassify;
        TextView tvTitle;
        TextView tvTime;

        public ArticleViewHolder(View itemView) {
            super(itemView);
            tvAuthor = itemView.findViewById(R.id.tv_author_item_article_adapter2);
            tvClassify = itemView.findViewById(R.id.tv_classify_item_article_adapter2);
    //            shift+f6 全局改名
            tvTitle = itemView.findViewById(R.id.tv_title_item_article_adapter2);
            tvTime = itemView.findViewById(R.id.tv_time_item_article_adapter2);
        }
    }

    static class BannerViewHolder extends RecyclerView.ViewHolder{

        public BannerViewHolder(View itemView) {
            super(itemView);
        }
    }

}
